(function () {
    let num = 0,
        threshold = 10,
        bool;
    getData()

    function getData() {
        num += 4;
        $.ajax({
            url: "http://chst.vip:1234/api/getinlanddiscount",
            method: "GET"
        }).then(res => {
            // 控制传过去的数据量
            res.result.length = num;
            createLi(res.result)
            //给li设置点击事件 跳转详情页
            $("main li").click(function () {
                let productId = $(this).attr("productId");
                location.href = `../pages/inlanddiscount_details.html?productId=${productId}`
            })
        })
    }

    function createLi(data) {
        let html = data.reduce((value, item) => {
            return value += `
                <li productId=${item.productId}>
                    <div class='img'>${item.productImg}</div>
                    <div class='title'><h4>${item.productName}</h4></div>  
                    <div class='price'><p>${item.productPrice}</P></div>
                    <div class='shop'><span>${item.productFrom}</span><i>${item.productTime}</i></div>
                </li>
            `
        }, "")
        $("main>ul").html(html)
    }
    //滚动事件 触发getData 获取下面数据
    $(window).scroll(function () {
        if (num > 20) return
        let sh = $("html")[0].scrollHeight;
        let st = $("html")[0].scrollTop;
        let ch = $("html")[0].clientHeight;
        let distance = sh - st - ch;
        if (distance < threshold) {
            if (bool) {
                bool = false;
                getData()
            }
        } else bool = true;
    })
    //返回顶部动漫效果
    $("footer>.nav li").eq(2).click(function () {
        $("main").stop().animate({
            scrollTop: 0
        }, 200)
    })
}())